<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateOrgTables extends Migrator
{
    public function change(): void
    {
        $this->createOrgTable();
        $this->createOrgProfileTable();
        $this->createOrgContactTable();
        $this->createOrgAddressTable();
        $this->createOrgMemberTable();
        $this->createOrgInvitationTable();
    }

    private function createOrgTable(): void
    {
        $this->table('org', [
            'comment' => '组织表（买家/卖家主体）',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('name', 'string', [
                'limit' => 150,
                'comment' => '组织名称',
            ])
            ->addColumn('org_type', 'string', [
                'limit' => 20,
                'default' => 'seller',
                'comment' => '组织类型 seller/buyer/both',
            ])
            ->addColumn('warehouse_code', 'string', [
                'limit' => 60,
                'default' => '',
                'comment' => '仓库代码（卖家展示用）',
            ])
            ->addColumn('logo_url', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => 'Logo 地址',
            ])
            ->addColumn('region_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '所属区域ID',
            ])
            ->addColumn('owner_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '组织负责人用户ID',
            ])
            ->addColumn('inviter_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '邀请人用户ID',
            ])
            ->addColumn('approved_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '审核人用户ID',
            ])
            ->addColumn('approved_at', 'timestamp', [
                'null' => true,
                'comment' => '审核时间',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '状态 0-草稿 1-启用 2-禁用',
            ])
            ->addColumn('extra', 'json', [
                'null' => true,
                'comment' => '扩展配置',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['org_type'])
            ->addIndex(['region_id'])
            ->addIndex(['owner_user_id'])
            ->addIndex(['inviter_user_id'])
            ->addIndex(['status'])
            ->create();
    }

    private function createOrgProfileTable(): void
    {
        $this->table('org_profile', [
            'comment' => '组织扩展资料',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'comment' => '组织ID',
            ])
            ->addColumn('license_number', 'string', [
                'limit' => 64,
                'default' => '',
                'comment' => '营业执照号',
            ])
            ->addColumn('license_photo', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '营业执照图片',
            ])
            ->addColumn('tax_number', 'string', [
                'limit' => 64,
                'default' => '',
                'comment' => '纳税人识别号',
            ])
            ->addColumn('legal_rep', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '法人代表',
            ])
            ->addColumn('registered_capital', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '注册资本',
            ])
            ->addColumn('biz_scope', 'text', [
                'null' => true,
                'comment' => '经营范围',
            ])
            ->addColumn('established_at', 'date', [
                'null' => true,
                'comment' => '成立日期',
            ])
            ->addColumn('extra', 'json', [
                'null' => true,
                'comment' => '其他信息',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['org_id'], ['unique' => true])
            ->create();
    }

    private function createOrgContactTable(): void
    {
        $this->table('org_contact', [
            'comment' => '组织联系人及对公信息',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'comment' => '组织ID',
            ])
            ->addColumn('contact_name', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '联系人姓名',
            ])
            ->addColumn('contact_phone', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '联系电话',
            ])
            ->addColumn('contact_email', 'string', [
                'limit' => 120,
                'default' => '',
                'comment' => '联系邮箱',
            ])
            ->addColumn('bank_name', 'string', [
                'limit' => 120,
                'default' => '',
                'comment' => '开户行',
            ])
            ->addColumn('bank_account', 'string', [
                'limit' => 64,
                'default' => '',
                'comment' => '银行账号',
            ])
            ->addColumn('bank_account_name', 'string', [
                'limit' => 120,
                'default' => '',
                'comment' => '开户名',
            ])
            ->addColumn('invoice_title', 'string', [
                'limit' => 150,
                'default' => '',
                'comment' => '发票抬头',
            ])
            ->addColumn('invoice_taxno', 'string', [
                'limit' => 64,
                'default' => '',
                'comment' => '税号',
            ])
            ->addColumn('extra', 'json', [
                'null' => true,
                'comment' => '扩展信息',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['org_id'], ['unique' => true])
            ->create();
    }

    private function createOrgAddressTable(): void
    {
        $this->table('org_address', [
            'comment' => '组织地址信息',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'comment' => '组织ID',
            ])
            ->addColumn('contact_name', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '联系人',
            ])
            ->addColumn('contact_phone', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '联系电话',
            ])
            ->addColumn('province', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '省份',
            ])
            ->addColumn('city', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '城市',
            ])
            ->addColumn('district', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '区县',
            ])
            ->addColumn('address', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '详细地址',
            ])
            ->addColumn('postal_code', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '邮编',
            ])
            ->addColumn('is_default', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '是否默认地址',
            ])
            ->addColumn('address_type', 'string', [
                'limit' => 20,
                'default' => 'shipping',
                'comment' => '地址类型 shipping/billing',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['org_id'])
            ->addIndex(['org_id', 'is_default'])
            ->create();
    }

    private function createOrgMemberTable(): void
    {
        $this->table('org_member', [
            'comment' => '组织成员关联',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'comment' => '组织ID',
            ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('member_role', 'string', [
                'limit' => 20,
                'default' => 'member',
                'comment' => '成员角色 owner/admin/member',
            ])
            ->addColumn('position', 'string', [
                'limit' => 50,
                'default' => '',
                'comment' => '职位',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-正常',
            ])
            ->addColumn('invited_by', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '邀请人',
            ])
            ->addColumn('joined_at', 'timestamp', [
                'null' => true,
                'comment' => '加入时间',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['org_id'])
            ->addIndex(['user_id'])
            ->addIndex(['org_id', 'user_id'], ['unique' => true])
            ->addIndex(['member_role'])
            ->create();
    }

    private function createOrgInvitationTable(): void
    {
        $this->table('org_invitation', [
            'comment' => '组织邀请记录',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('org_id', 'integer', [
                'signed' => false,
                'comment' => '组织ID',
            ])
            ->addColumn('inviter_user_id', 'integer', [
                'signed' => false,
                'comment' => '邀请人用户ID',
            ])
            ->addColumn('invitee_user_id', 'integer', [
                'signed' => false,
                'null' => true,
                'comment' => '被邀请用户ID（注册后回填）',
            ])
            ->addColumn('invitee_phone', 'string', [
                'limit' => 20,
                'default' => '',
                'comment' => '被邀请人手机号',
            ])
            ->addColumn('invitee_email', 'string', [
                'limit' => 120,
                'default' => '',
                'comment' => '被邀请人邮箱',
            ])
            ->addColumn('invite_code', 'string', [
                'limit' => 64,
                'comment' => '邀请码',
            ])
            ->addColumn('status', 'string', [
                'limit' => 20,
                'default' => 'pending',
                'comment' => '状态 pending/accepted/expired/rejected',
            ])
            ->addColumn('expires_time', 'datetime', [
                'null' => true,
                'comment' => '过期时间',
            ])
            ->addColumn('accepted_time', 'datetime', [
                'null' => true,
                'comment' => '接受时间',
            ])
            ->addColumn('remark', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '备注',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['org_id'])
            ->addIndex(['inviter_user_id'])
            ->addIndex(['invite_code'], ['unique' => true])
            ->addIndex(['status'])
            ->create();
    }
}
